angular.module('U9ImPDA.controllers')

    .controller('HomeCtrl', ['$scope', '$ionicSlideBoxDelegate', '$ionicModal', '$ionicHistory', '$state', 'AppManager', 'Device', 'User', 'Help', 'imanDialog', 'APPCONSTANTS',
        function ($scope, $ionicSlideBoxDelegate, $ionicModal, $ionicHistory, $state, AppManager, Device, User, Help, imanDialog, APPCONSTANTS) {
            $scope.data = {
                user: User.get(),
                minCtHeight: {
                    'height': '380px'
                }
            };

            $scope.data.appsArray = formatApps(AppManager.getApps());

            $ionicModal.fromTemplateUrl('tpls/sidemenu.html', {
                scope: $scope,
                animation: 'slide-in-left'
            }).then(function (modal) {
                $scope.modal = modal;
            });

            $scope.showSideMenu = function () {
                $scope.modal.show();
            };
            $scope.onPagerClick = function (index) {
                $ionicSlideBoxDelegate.slide(index);
            };
            $scope.clickApp = function (app) {
                if (!app.NoWorkStation && !Device.get().WorkLocation) {
                    imanDialog.confirm('请到终端注册界面进行工位绑定', '工位绑定').then(function (btn) {
                        if (btn) {
                            $state.go('register');
                        }
                    });
                    return;
                }
                Help.loadApp(app.AppID).then(function () {
                    $state.go(app.AppID);
                }, function () {
                    imanDialog.toastCenter('加载应用失败，请确认是否已正确安装然后重试。');
                });
            };

            $scope.register = function () {
                $scope.modal.hide().then(function () {
                    $state.go('register');
                });
            };
            $scope.portSetting = function () {
                $scope.modal.hide().then(function () {
                    $state.go('portsetting');
                });
            };
            $scope.connectSetting = function () {
                $scope.modal.hide().then(function () {
                    $state.go('connectsetting');
                });
            };
            $scope.signout = function () {
                $scope.modal.hide().then(function () {
                    User.signout().then(function () {
                        $ionicHistory.nextViewOptions({
                            historyRoot: true,
                            disableAnimate: true,
                            expire: 300
                        });
                        $state.go('login');
                    });
                });
            };

            $scope.$on('$destroy', function () {
                $scope.modal.remove();
            });

            function formatApps (apps) {
                var viewContainer = angular.element('.view-container'),
                    maxNum = APPCONSTANTS.appNumPerPage;
                if (viewContainer.length > 0) {
                    var appHeight = 105,
                        deltaHeight = 140,
                        row = Math.floor((viewContainer.height() - deltaHeight) / appHeight),
                        col = 3;
                    if (row > 0) {
                        maxNum = row * col;
                        $scope.data.minCtHeight['height'] = appHeight * row + 'px';
                    }
                } 
                var j = 0,
                    temp = [],
                    results = [];
                for (var i in apps) {
                    temp.push(apps[i]);
                    j++;
                    if (j === maxNum) {
                        j = 0;
                        results.push(temp);
                        temp = [];
                    }
                }
                if (apps.length % maxNum !== 0) {
                    results.push(temp);
                }
                return results;
            }
        }
    ]);